<?php

class ExceptionApp extends BackendApp
{
	var $_exception_mod;
	var $_operator_mod;
	var $_exattr_mod;
	function __construct()
	{
		$this->ExceptionApp();
	}
	function ExceptionApp()
	{
		parent::BackendApp();

		$this->_exception_mod =& m('exception');
		$this->_operator_mod =& m('operator');
		$this->_exattr_mod =& m('exattr');
	}

	/* 管理 */
	function index()
	{
		$db=&db();
		$sql="SELECT d_key,d_value FROM zys_dictionary WHERE d_code='extype'";
		$types=array();
		$temp=$db->getAll($sql);
		foreach ($temp as $v){
			$types[$v['d_key']]=$v['d_value'];
		}
		$this->assign('types', $types);

		$wm_carrier=empty($_GET['wm_carrier'])?'':$_GET['wm_carrier'];
		$type_id=empty($_GET['type_id'])?'':$_GET['type_id'];
		$e_content=empty($_GET['e_content'])?'':$_GET['e_content'];
		$e_bill_code=empty($_GET['e_bill_code'])?'':$_GET['e_bill_code'];
		$conditions ='';
		if($wm_carrier){
			$conditions .= " AND user_name LIKE '%".$wm_carrier."%'";
		}
		if($type_id){
			$conditions .= " AND e_type = '".$type_id."'";
		}
		if($e_content){
			$conditions .= " AND e_content LIKE '%".$e_content."%'";
		}
		if($e_bill_code){
			$conditions .= " AND e_bill_code LIKE '%".$e_bill_code."%'";
		}
		$sort  = 'e_op_date';
		$order = 'desc';

		$page   =   $this->_get_page(10);   //获取分页信息
		$user_ids = $this->checkpower();
		if(!empty($user_ids)){
			$waybillsql = "select  group_concat(wm_code) from zys_waybill_main where wm_optid in(".$user_ids.")";
			$wmcodes = $db->getOne($waybillsql);
			if(!empty($wmcodes)){
				$conditions .= " AND e_bill_code in (".$wmcodes.")";
				$exceptions=$this->_exception_mod->find(array(
					'fields'   => 'this.*,member.user_name,member.real_name,member.vhcCode,member.phone_tel',
					'conditions'  => '1=1 '.$conditions,
					'limit'   => $page['limit'],
					'join'    => 'belongs_to_operator',
					'order'   => "$sort $order", //必须加别名
					'count'   => true   //允许统计
				));
				$page['item_count']=$this->_exception_mod->getCount();   //获取统计数据
			}else{
				$exceptions = array();
				$page['item_count']=0;
			}
		}

		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('exceptions', $exceptions);

		$this->assign('tabself', json_encode(array('text'=>'异常管理')));

		$this->display('exception.index.html');
	}




	//编辑
	function edit()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

		if (!IS_POST)
		{
			$exception=$this->_exception_mod->get(array(
	        'fields'   => 'this.*,member.user_name',
	        'conditions'  => 'e_tid='.$id,
			'join'    => 'belongs_to_operator',
			));

			$exattr=$this->_exattr_mod->find(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'ea_e_tid='.$id,

			));
			$this->assign('id', $id);
				
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        	'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
				

			$this->assign('waybill', $exception);
			$this->assign('exattr', $exattr);

			$this->assign('tabself', json_encode(array('text'=>'异常详情')));
			$this->assign('tabback', json_encode(array('text'=>'运单管理','url'=>'')));

			$this->display('exception.form.html');
		}else {
			$db=&db();
			$p=empty($_POST['e_handle_person'])?'':$_POST['e_handle_person'];
			$d=empty($_POST['e_handle_date'])?'':$_POST['e_handle_date'];
			$note=empty($_POST['e_handle_note'])?'':$_POST['e_handle_note'];


			$sql="UPDATE zys_exception SET e_staus='已处理',e_handle_person='".$p."'
					,e_handle_date='".gmstr2time($d)."',e_handle_note='".$note."' WHERE e_tid=".$id."";

			$db->query($sql);

			$this->show_message('处理成功',
                '返回异常',   'index.php?app=exception&act=edit&id=' . $id
				);
		}


	}

	function view()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

		if (!IS_POST)
		{
			$exception=$this->_exception_mod->get(array(
	        'fields'   => 'this.*,member.user_name',
	        'conditions'  => 'e_tid='.$id,
			'join'    => 'belongs_to_operator',
			));
			$exattr=$this->_exattr_mod->find(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'ea_e_tid='.$id,

			));
			$this->assign('id', $id);

			$this->assign('waybill', $exception);
			$this->assign('exattr', $exattr);

			$this->display('exception.view.html');
		}

	}
}
